<?php

namespace Cpadmin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Cpadmin\Model\Admin;
use Zend\Session\Container;

class IndexController extends AbstractActionController {

    private $AdminTable;

    private function getAdminTable() {
        if (!$this->AdminTable) {
            $pst = $this->getServiceLocator();
            $this->AdminTable = $pst->get("Cpadmin\Model\AdminTable");
        }
        return $this->AdminTable;
    }

    public function indexAction() {
        $this->layout("layout/layoutlogin");
        $session = new Container("acesslogin");
        if ($session->count == 5) {
            $this->redirect()->toUrl(WEB_PATH . "/access.html");
        }
    }

    public function loginAction() {
        //$this->layout("layout/layout");
        $checkAction = $this->params()->fromPost("btn");
        if ($checkAction != null) {
            $username = addslashes(trim($this->params()->fromPost("username")));
            $password = addslashes(trim($this->params()->fromPost("password")));
            $objadmin = new Admin();
            $objadmin->setAdminUsername($username);
            $objadmin->setAdminpassword($password);
            $check = $this->getAdminTable()->checkLogin($objadmin);
            if ($check) {
                $urlok = WEB_PATH . "/systemadmin";
                echo "10*duypc*" . $urlok;
            } else {
                $session = new Container("acesslogin");
                $session->count = ( $session->count) + 1;

                echo $session->count . "*duypc*" . WEB_PATH . "/access.html";
            }
        }
        die;
    }

    public function logoutAction() {
        $this->layout("layout/layoutlogin");
        $session = new Container("ssadmin");
        $session->adminusername = "";
        $this->redirect()->toUrl(WEB_PATH . "/cpadmin");
    }

    public function changepassAction() {
        $password = addslashes(trim($this->params()->fromPost("password")));
        $passwordnew = addslashes(trim($this->params()->fromPost("passwordnew")));
        $passwordconfirm = addslashes(trim($this->params()->fromPost("passwordconfirm")));
        $checkpass = $this->getAdminTable()->checkpass($password);
        if ($checkpass) {
            if ($passwordnew != null && $passwordconfirm != null && strlen($passwordconfirm) > 6) {
                $admin = new Admin();
                $admin->setAdminpassword($passwordnew);
                $changepasscheck = $this->getAdminTable()->changepasstable($admin);
                if ($changepasscheck) {
                    echo "Bạn đã thay đổi mật khẩu thành công.";
                } else {
                    echo "Hệ thống đang bận. Bạn vui lòng thử lại sau.";
                }
            } else {
                echo "Bạn chưa nhập đủ thông tin.";
            }
        } else {
            echo "Mật khẩu bạn nhập vào không chính xác.";
        }
        die;
    }

}
